package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.OceanFeeBiz;
import com.egao.orderB.entity.OceanFee;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2020-12-04 16:07:57
 */
@Repository
public class OceanFeeBizImpl extends BaseDao implements OceanFeeBiz {

  @Override
  public JsonResult list(PageResult2<T> page, OceanFee oceanFee, User loginUser) throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<OceanFee> rm = new BeanPropertyRowMapper<OceanFee>(OceanFee.class);
    StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.ocean_id");
    sql.append(",o.type,o.fee_name,o.price,o.standard");
    sql.append(",o.fee_state,o.fee_person,o.fee_time,o.fobject");
    sql.append(",o.invoiceid,o.invoicename,o.driver_name");
    sql.append(",o.invoicestate,o.currency,o.exchange_rate,o.remark");
    sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_fee o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id ");
    sql.append(" where o.deleted=0 and sogzn.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(oceanFee)) {
      if (Utils.isNotEmpty(oceanFee.getOceanId())) {
        sql.append(" and o.ocean_id=:ocean_id ");
        map.put("ocean_id", oceanFee.getOceanId());
      }
//			if(Utils.isNotEmpty(oceanFee.getFeeName())){
//				sql.append(" and instr(o.name,:name)>0 ");
//				map.put("name", oceanFee.getFeeName());
//			}
      if (Utils.isNotEmpty(oceanFee.getSort()) && Utils.isNotEmpty(oceanFee.getOrder())) {
        sql.append(" order by o." + oceanFee.getSort() + " " + oceanFee.getOrder());
      } else {
        sql.append(" order by o.create_time desc");
      }
    }
    return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
  }

  @Override
  public List<OceanFee> list(OceanFee oceanFee, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<OceanFee> rm = new BeanPropertyRowMapper<OceanFee>(OceanFee.class);
    StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.ocean_id");
    sql.append(",o.type,o.fee_name,o.price,o.standard");
    sql.append(",o.fee_state,o.fee_person,o.fee_time,o.fobject");
    sql.append(",o.invoiceid,o.invoicename,o.driver_name");
    sql.append(",o.invoicestate,o.currency,o.exchange_rate,o.remark");
    sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_fee o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id ");
    sql.append(" where o.deleted=0 and sogzn.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(oceanFee)) {
//			if(Utils.isNotEmpty(oceanFee.getName())){
//				sql.append(" and instr(o.name,:name)>0 ");
//				map.put("name", oceanFee.getName());
//			}
      if (Utils.isNotEmpty(oceanFee.getType())) {
        sql.append(" and o.type=:type ");
        map.put("type", oceanFee.getType());
      }
      if (Utils.isNotEmpty(oceanFee.getOceanId())) {
        sql.append(" and o.ocean_id=:ocean_id ");
        map.put("ocean_id", oceanFee.getOceanId());
      }
      if (Utils.isNotEmpty(oceanFee.getSort()) && Utils.isNotEmpty(oceanFee.getOrder())) {
        sql.append(" order by o." + oceanFee.getSort() + " " + oceanFee.getOrder());
      } else {
        sql.append(" order by o.create_time desc");
      }
    }
    getMapParameter(sql, map);
    return jdbcTemplate.query(sql.toString(), map, rm);
  }

  @Override
  public List<OceanFee> listExcel(OceanFee oceanFee, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<OceanFee> rm = new BeanPropertyRowMapper<OceanFee>(OceanFee.class);
    StringBuffer sql = new StringBuffer("");
    sql.append("select type,currency,sum(price) price ");
    sql.append(" from chf_ocean_fee ");
    sql.append(" where ocean_id=:ocean_id and deleted=0");
    sql.append(" GROUP BY type,currency ");
    map.put("ocean_id", oceanFee.getOceanId());
    getMapParameter(sql, map);
    return jdbcTemplate.query(sql.toString(), map, rm);
  }

  @Override
  public OceanFee getById(OceanFee oceanFee) {
    RowMapper<OceanFee> rm = new BeanPropertyRowMapper<OceanFee>(OceanFee.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.ocean_id");
    sql.append(",o.type,o.fee_name,o.price,o.standard");
    sql.append(",o.fee_state,o.fee_person,o.fee_time,o.fobject");
    sql.append(",o.invoiceid,o.invoicename,o.driver_name");
    sql.append(",o.invoicestate,o.currency,o.exchange_rate,o.remark");
    sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_fee o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id ");
    sql.append(" where o.deleted=0 and sogzn.deleted=0 and o.id=:id ");
    map.put("id", oceanFee.getId());
    getMapParameter(sql, map);
    return jdbcTemplate.queryForObject(sql.toString(), map, rm);
  }

  @Override
  public boolean updateFeeState(OceanFee oceanFee, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_ocean_fee set ");
    sql.append("fee_state=:fee_state,fee_person=:fee_person,fee_time=:fee_time ");
    sql.append(",update_user=:update_user,update_time=now() where id=:id ");
    map.put("fee_state", oceanFee.getFeeState());
    map.put("fee_person", oceanFee.getFeePerson());
    map.put("fee_time", oceanFee.getFeeTime());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", oceanFee.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean updateFeeStateByBill(OceanFee oceanFee, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_ocean_fee set ");
    sql.append("fee_state=:fee_state,fee_person=:fee_person,fee_time=:fee_time ");
    sql.append(",update_user=:update_user,update_time=now() where ocean_id=:ocean_id ");
    map.put("fee_state", oceanFee.getFeeState());
    map.put("fee_person", oceanFee.getFeePerson());
    map.put("fee_time", oceanFee.getFeeTime());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("ocean_id", oceanFee.getOceanId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean save(OceanFee oceanFee, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("insert into chf_ocean_fee (");
    sql.append("organization_id,ocean_id,type,fee_name");
    sql.append(",price,standard,fee_state,fee_person");
    sql.append(",fee_time,fobject,invoiceid,invoicename");
    sql.append(",driver_name,invoicestate,currency");
    sql.append(",exchange_rate,remark,create_user,create_time) values ( ");
    sql.append(":organization_id,:ocean_id,:type,:fee_name");
    sql.append(",:price,:standard,:fee_state,:fee_person");
    sql.append(",:fee_time,:fobject,:invoiceid,:invoicename");
    sql.append(",:driver_name,:invoicestate,:currency");
    sql.append(",:exchange_rate,:remark,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("ocean_id", oceanFee.getOceanId());
    map.put("type", oceanFee.getType());
    map.put("fee_name", oceanFee.getFeeName());
    map.put("price", oceanFee.getPrice());
    map.put("standard", oceanFee.getStandard());
    map.put("fee_state", oceanFee.getFeeState());
    map.put("fee_person", oceanFee.getFeePerson());
    map.put("fee_time", oceanFee.getFeeTime());
    map.put("fobject", oceanFee.getFobject());
    map.put("invoiceid", oceanFee.getInvoiceid());
    map.put("invoicename", oceanFee.getInvoicename());
    map.put("driver_name", oceanFee.getDriverName());
    map.put("invoicestate", oceanFee.getInvoicestate());
    map.put("currency", oceanFee.getCurrency());
    map.put("exchange_rate", oceanFee.getExchangeRate());
    map.put("remark", oceanFee.getRemark());
    map.put("create_user", loginUser.getUserId() + "");
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean update(OceanFee oceanFee, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_ocean_fee set ");
    sql.append("type=:type,fee_name=:fee_name ");
    sql.append(",price=:price,standard=:standard,fee_state=:fee_state ");
    sql.append(",fee_person=:fee_person,fee_time=:fee_time,fobject=:fobject ");
    sql.append(",invoiceid=:invoiceid,invoicename=:invoicename,driver_name=:driver_name ");
    sql.append(",invoicestate=:invoicestate,currency=:currency ");
    sql.append(",exchange_rate=:exchange_rate,remark=:remark,update_time=now() ");
    sql.append(",update_user=:update_user where id=:id ");
    map.put("type", oceanFee.getType());
    map.put("fee_name", oceanFee.getFeeName());
    map.put("price", oceanFee.getPrice());
    map.put("standard", oceanFee.getStandard());
    map.put("fee_state", oceanFee.getFeeState());
    map.put("fee_person", oceanFee.getFeePerson());
    map.put("fee_time", oceanFee.getFeeTime());
    map.put("fobject", oceanFee.getFobject());
    map.put("invoiceid", oceanFee.getInvoiceid());
    map.put("invoicename", oceanFee.getInvoicename());
    map.put("driver_name", oceanFee.getDriverName());
    map.put("invoicestate", oceanFee.getInvoicestate());
    map.put("currency", oceanFee.getCurrency());
    map.put("exchange_rate", oceanFee.getExchangeRate());
    map.put("remark", oceanFee.getRemark());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", oceanFee.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean remove(OceanFee oceanFee, User loginUser) {
    int count = 0;
    Map<String, String> map = null;
    if (oceanFee.getId().length() > 0) {
      for (String ID : oceanFee.getId().split(",")) {
        map = new HashMap<String, String>();
        if (ID == null || ID.equals("")) {
          break;
        }
        StringBuffer sql = new StringBuffer("update chf_ocean_fee set deleted=1" +
                ",update_user=:update_user, update_time=now() "
                + " where id =:id ");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", ID);
        getMapParameter(sql, map);
        count = jdbcTemplate.update(sql.toString(), map);
      }
    }
    return count > 0;
  }
}
